<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增课程</el-button>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="courseList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="60"/>
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课程描述" align="center"  prop="des">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.des" placement="top">
            <p class="showOverTooltip">{{scope.row.des}}</p>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="教师" align="center" prop="teacherName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="courseItem = scope.row;choseVisible = true;"
          >上线课程</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="updateCourse(scope.row)"
          >修改课程</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程描述" prop="des">
          <el-input :rows="3" type="textarea" v-model="form.des" placeholder="请输入课程描述" />
        </el-form-item>
        <el-form-item label="教师" prop="teacherName">
          <!--          <el-input v-model="form.teacherName" placeholder="请输入老师" />-->
          <el-select v-model="form.teacherId" placeholder="请选择教师" clearable>
            <el-option
              v-for="dict in teachDictList"
              :key="dict.teacherId"
              :label="dict.name"
              :value="dict.teacherId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="提示"
      :visible.sync="choseVisible"
      width="30%"
      center>
      <span>确定选择{{courseItem==null?'‘无’':courseItem.courseName}}课程上线吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="choseVisible = false">取 消</el-button>
        <el-button type="primary" @click="choseCourse">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import {added, getAdded, getTeachList,getUnAdded, saveUnAdded, unAdded,updateCourse} from "@/api/courseSystem/admin";

export default {
  name: "downCourse",
  data() {
    return {
      teachDictList:[],
      courseItem:null,
      choseVisible:false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        des: null,
        courseName: null,
        teacherId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        des: [
          { required: true, message: "课程描述不能为空", trigger: "blur" }
        ],
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    makeCourse(row){

    },
    choseCourse(){
      if (this.courseItem!=null){
        added(this.courseItem.courseId).then(res=>{
          if (res.code!=200){
            this.$message.error('上线失败啦')
          }else {
            this.$message.success('上线成功')
          }
          this.choseVisible = false;
          this.getList();
        })
      }
    },
    updateCourse(row){
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true;
      this.title = "修改课程";
    },
    getList() {
      this.loading = true;
      getUnAdded(this.queryParams).then(res=>{
        if (res.code==200){
          this.courseList = res.data.rows;
          this.total = res.data.total;
          this.$message.success('查询成功');
        }else {
          this.$message.error('查询出错啦');
        }
        this.loading = false;
      })
      getTeachList().then(res=>{
        this.teachDictList = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        des: null,
        courseName: null,
        teacherId: null,
        teachType: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId==null||this.form.courseId==''){
            saveUnAdded(this.form).then(res=>{
              if (res.code==200){
                this.$message.success('下架新增成功')
                this.getList();
              }else {
                this.$message.error('新增失败了啦')
              }
              this.open = false;
            })
          }else {
            updateCourse(this.form).then(res=>{
              if (res.code==200){
                this.$message.success('修改成功')
                this.getList();
              }else {
                this.$message.error('修改失败了啦')
              }
              this.open = false;
            })
          }

        }
      });
    },

  }
};
</script>
<style scoped>
.showOverTooltip{
  display:-webkit-box;
  text-overflow:ellipsis;
  overflow:hidden;
  /*这里是3行*/
  -webkit-line-clamp: 3;
  -webkit-box-orient:vertical;
}
</style>
